using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.DirectDraw;
using TowerDefense_Dx.Core;

namespace TowerDefense_Dx.GameLogic
{
   public class WaveManager
    {
       private DxInitGraphics _graphics;
       private int _numOfWave;
       private float _timeSinceLastWave;
       public float WaveTime
       {
           get { return _timeSinceLastWave; }
       }
       private Queue<Wave> _waves = new Queue<Wave>();
       private bool _isWaveFinish = false;
       public Wave CurrentWave
       {
           get { return _waves.Peek(); }
       }
       public List<Enemy> Enemies
       {
           get { return CurrentWave.EnemyList; }
       }
       public int Round
       {
           get { return CurrentWave.WaveNumber + 1; }
       }
       private int[] _wayPoint;

       public int[] WayPoint
       {
           get { return _wayPoint; }
           set { _wayPoint = value; }
       }


       public WaveManager(DxInitGraphics graphics,string enemyFilePath, int numOfWave,int[] wayPoint)
       {
           _graphics = graphics;
           _numOfWave = numOfWave;
           _wayPoint = wayPoint;
           for (int i = 0; i < _numOfWave; i++)
           {
               int initNumOfEnemy = 6;
               int modNumofEnemy = (i/5) + 1;
               Wave wave = new Wave(_graphics,i + 1, enemyFilePath, initNumOfEnemy * modNumofEnemy, 1000,_wayPoint);               
               _waves.Enqueue(wave);
           }
           StartNextWave();
       }
       public void StartNextWave()
       {
           if (_waves.Count > 0)
           {
               _waves.Peek().Start();
               _timeSinceLastWave = 0;
              // _isWaveFinish = false;
           }
       }
       public void Update(double deltatime,Player player)
       {
           CurrentWave.EnemyUpdate(deltatime, player);
           if (CurrentWave.RoundOver)
           {
               _timeSinceLastWave += (float)deltatime;
           }
           if (_timeSinceLastWave >= 20000)
           {
               _waves.Dequeue();               
               StartNextWave();
           }
       }
       public void Draw(Surface destSurface)
       {
           CurrentWave.Draw(destSurface);
       }
   }
}
